package com.shuai.chatroom.common;

import com.shuai.chatroom.entity.UserInfo;
import com.shuai.chatroom.exception.ControllerException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

public class UserSessionUtils {
    /**
     * 获取HttpSession中在登录时存储的用户信息
     */
    public static UserInfo getUserInHttpSession(HttpServletRequest request) throws ControllerException {
        if (request == null || request.getSession(false) == null) {
            throw new ControllerException("非法请求！");
        }
        HttpSession session = request.getSession(false);
        Object attribute = session.getAttribute(AppConstant.USER_SESSION_KEY);
        if (attribute == null || ((UserInfo) attribute).getUId() == null) {
            throw new ControllerException("非法请求！");
        }
        return ((UserInfo) attribute);
    }
}
